package com.ruoyi.common.utils.appsdks;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author wangxianl
 * createTime: 2023/4/6.
 * description:
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "minio", name = "enable", havingValue = "true")
public class MinioUtil {

    /**
     * 服务地址
     */
    private static String apiUrl;

    /**
     * 文件地址
     */
    private static String consoleUrl;


    /**
     * 用户名
     */
    private static String accessKey;

    /**
     * 密码
     */
    private static String secretKey;

    /**
     * 存储桶名称
     */
    private static String bucketName;

    @PostConstruct
    public void init() {
        apiUrl = SpringUtils.getRequiredProperty("minio.apiUrl");
        consoleUrl = SpringUtils.getRequiredProperty("minio.consoleUrl");
        accessKey = SpringUtils.getRequiredProperty("minio.accessKey");
        secretKey = SpringUtils.getRequiredProperty("minio.secretKey");
        bucketName = SpringUtils.getRequiredProperty("minio.bucketName");
    }

    @Bean
    public MinioClient getMinioClient() {
        return MinioClient.builder().endpoint(getApiUrl()).credentials(getAccessKey(), getSecretKey()).build();
    }

    /**
     * 上传文件
     *
     * @param bucketName    桶名称
     * @param fileName      文件名
     * @param multipartFile 文件上传
     */
    public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {
        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .stream(inputStream, multipartFile.getSize(), -1)
                            .contentType(multipartFile.getContentType())
                            .build()
            );
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());
            url = url.substring(0, url.indexOf('?'));
            url = url.replace(apiUrl, consoleUrl);
            return ServletUtils.urlDecode(url);
        } catch (Exception e) {
            log.error("minio上传", e);
            throw new IOException(e.getMessage(), e);
        }
    }

    public static String getApiUrl() {
        return apiUrl;
    }

    public static String getConsoleUrl() {
        return consoleUrl;
    }

    public static String getAccessKey() {
        return accessKey;
    }

    public static String getSecretKey() {
        return secretKey;
    }

    public static String getBucketName() {
        return bucketName;
    }
}
